<?php
/**
 * API会员逻辑
 * @author 王勇程 <laughskydragon@qq.com>
 */

namespace app\api\logic;

class Member extends ApiBase
{
    /**
     * 获取会员套餐配置
     */
    public function getMemberPlans($data = [])
    {
        // 1. JWT解析获取user_id（如果需要）
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->getMemberPlans($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 获取最近会员
     */
    public function getRecentMembers($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用用户逻辑层
        $result = $this->logicUser->getRecentMembers($data);
        
        // API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 创建VIP订单
     */
    public function createVipOrder($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户未登录'];
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->createVipOrder($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    

    
    /**
     * 获取用户会员信息
     */
    public function getUserMemberInfo($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户未登录'];
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->getUserMemberInfo($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 获取会员权益
     */
    public function getMemberBenefits($data = [])
    {
        // 1. JWT解析获取user_id（如果需要）
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->getMemberBenefits($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 验证会员状态
     */
    public function validateMemberStatus($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户未登录'];
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->validateMemberStatus($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 获取会员订单列表
     */
    public function getMemberOrders($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户未登录'];
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->getMemberOrders($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 获取订单详情
     */
    public function getOrderDetail($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户未登录'];
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->getOrderDetail($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
    
    /**
     * 取消订单
     */
    public function cancelOrder($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            if ($userData) {
                $data['user_id'] = $userData['user_id'];
            }
        }
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户未登录'];
        }
        
        // 2. 调用用户逻辑层
        $result = $this->logicUser->cancelOrder($data);
        
        // 3. API逻辑层直接返回common层的结果
        return $result;
    }
}